import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import * as moment from 'moment';
import { DATE_FORMAT } from 'app/shared/constants/input.constants';
import { map } from 'rxjs/operators';

import { createRequestOption } from 'app/shared';
import { IDeliverOffer } from 'app/shared/model/deliver-offer.model';

type EntityResponseType = HttpResponse<IDeliverOffer>;
type EntityArrayResponseType = HttpResponse<IDeliverOffer[]>;

@Injectable({ providedIn: 'root' })
export class DeliverOfferService {
    private resourceUrl = 'api/deliver-offers';

    constructor(private http: HttpClient) {}

    create(deliverOffer: IDeliverOffer): Observable<EntityResponseType> {
        const copy = this.convertDateFromClient(deliverOffer);
        return this.http
            .post<IDeliverOffer>(this.resourceUrl, copy, { observe: 'response' })
            .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
    }

    update(deliverOffer: IDeliverOffer): Observable<EntityResponseType> {
        const copy = this.convertDateFromClient(deliverOffer);
        return this.http
            .put<IDeliverOffer>(this.resourceUrl, copy, { observe: 'response' })
            .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
    }

    find(id: number): Observable<EntityResponseType> {
        return this.http
            .get<IDeliverOffer>(`${this.resourceUrl}/${id}`, { observe: 'response' })
            .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
    }

    query(req?: any): Observable<EntityArrayResponseType> {
        const options = createRequestOption(req);
        return this.http
            .get<IDeliverOffer[]>(this.resourceUrl, { params: options, observe: 'response' })
            .pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
    }

    delete(id: number): Observable<HttpResponse<any>> {
        return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response' });
    }

    private convertDateFromClient(deliverOffer: IDeliverOffer): IDeliverOffer {
        const copy: IDeliverOffer = Object.assign({}, deliverOffer, {
            leaveDate: deliverOffer.leaveDate != null && deliverOffer.leaveDate.isValid() ? deliverOffer.leaveDate.toJSON() : null,
            arriveDate: deliverOffer.arriveDate != null && deliverOffer.arriveDate.isValid() ? deliverOffer.arriveDate.toJSON() : null,
            createdDate: deliverOffer.createdDate != null && deliverOffer.createdDate.isValid() ? deliverOffer.createdDate.toJSON() : null
        });
        return copy;
    }

    private convertDateFromServer(res: EntityResponseType): EntityResponseType {
        res.body.leaveDate = res.body.leaveDate != null ? moment(res.body.leaveDate) : null;
        res.body.arriveDate = res.body.arriveDate != null ? moment(res.body.arriveDate) : null;
        res.body.createdDate = res.body.createdDate != null ? moment(res.body.createdDate) : null;
        return res;
    }

    private convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
        res.body.forEach((deliverOffer: IDeliverOffer) => {
            deliverOffer.leaveDate = deliverOffer.leaveDate != null ? moment(deliverOffer.leaveDate) : null;
            deliverOffer.arriveDate = deliverOffer.arriveDate != null ? moment(deliverOffer.arriveDate) : null;
            deliverOffer.createdDate = deliverOffer.createdDate != null ? moment(deliverOffer.createdDate) : null;
        });
        return res;
    }
}
